﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WPFClient
{
    /// <summary>
    /// Allows user to create new account.
    /// As navigation bar is active, user can return to log in screen by pressing back button.
    /// </summary>
    public partial class New_Account : Page
    {
        ChatClient client; //gets client from MainWindow
        public delegate void LoggedInHandler();
        public event LoggedInHandler LoggedIn;
        public New_Account(ChatClient _client)
        {
            InitializeComponent();
            client = _client; //gets client from MainWindow
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //on button clicked

            try
            {
            bool success;
            if (passwordBox1.Password.CompareTo("") != 0 && passwordBox2.Password.CompareTo("") != 0) //if neither fields are blanked
            {
                success = client.NewAccount(textBox1.Text, passwordBox1.Password,passwordBox2.Password); //tries to create account
                //with username and passwords given, success will determine whether it was successful or not
                //failure to create account is either because it already exists or because passwords don't match
            }
            else //if either field is blank
            {
                success = client.NewAccount(textBox1.Text, "null", "nuller"); //attempts an account creation that will surely fail
            }
                if (success) //if formation was successful, client will have also logged in to the new account
                {
                    LoggedIn(); //sends signal to MainWindow to open main page
                }
                else //if formation was unsuccessful
                {
                    textBlock3.Text = "Account formation failed. Try again."; //informs user
                }
            }
            catch (Exception) { //if failure in connection
                textBlock3.Text = "Could not connect to server. Try again later."; //informs user
            }
        }
    }
}
